package com.zthz.douxing.convert

import com.zthz.douxing.utils.CommonUtil
import com.zthz.douxing.utils.FetchDataUtils

/**
 * 将天气的数据和PM2.5数据进行混合。最终返回给最终前端。
 */
class WeathPmCabinService {

    def redisService
    def gsonBuilder
    def commonUtilService

    def static PM_WEATHER_CABIN_KEY = "PM_WEATHER_CABIN_KEY"

    def convert(def source, param = [:] ) {
        def cityCode = param["cityCode"]
        HashMap target = param.target
        //source 入参是map
        if(source instanceof Map && cityCode != null){
            def pm25 = FetchDataUtils.getKey([type:FetchDataUtils.TYPE_PM25,
                                        date:param.nowDate,cityCode:cityCode])


            if(target&&target.size()>0){
                def result = commonUtilService.getDateFromRedis(pm25)
                if(result){
                    def _json = commonUtilService.getDataFromJson(result)
                    if(_json){
                        target["pm_aqi"]= _json.aqi
                        target["pm_level"] = _json.level
                        target["pm_density"]=_json.density
                        def value = CommonUtil.getJSONFromateDataFromMap(target)
                        commonUtilService.save2RedisWithExpireTime(PM_WEATHER_CABIN_KEY+"_${cityCode}",value,4 * 60 * 60)
                    }
                }
            }
           return param
        }
    }
}

